#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sqlite3.h>
#include <time.h>
#define  N  128

#define DATABASE  "mysql.db"
#define TABLES "fruit"


//添加
void insert(sqlite3 *db);

//更新重量
void update_weight(sqlite3 *db);

//更新价格
void update_price(sqlite3 *db);

//查看库存
void query(sqlite3 *db);

//获取当前时间
void gettime(char *buf); 

int callback(void *arg, int f_num, char ** f_value, char ** f_name);


int flag  = 1;

int main(int argc, const char* argv[])
{
    sqlite3 *db;
    char sql[N] = {0};
    char *errmsg;
    int n;

    if(sqlite3_open(DATABASE, &db) != SQLITE_OK)
    {
	printf("%s\n", sqlite3_errmsg(db));
	exit(1);
    }


    sprintf(sql,"create table if not exists '%s' (id integer primary key autoincrement,name char,weight float,price float,time char);",TABLES);
    if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
    {
	printf("%s\n", errmsg);
    }

    query(db);

    while(1)
    {
	printf("1: 添加  2:查看库存  3:修改重量  4:修改价格 5:退出\n");
	printf("Please select:");
	scanf("%d", &n);

	switch(n)
	{
	    case 1:
		insert(db);
		break;
	    case 2:
		flag = 1;
		query(db);
		break;
	    case 3:
		update_weight(db);
		break;
	    case 4:
		update_price(db);
		break;
	    case 5:
		sqlite3_close(db);
		exit(0);
		break;
	    default :
		printf("Invalid data \n");
	}
    }




}


void gettime(char *buf)
{
    time_t t;
    struct tm *tp;
    time(&t);
    tp = localtime(&t);
    sprintf(buf,"%d-%02d-%02d %02d:%02d:%02d\n",
	    tp->tm_year+1900, tp->tm_mon+1,tp->tm_mday, tp->tm_hour, tp->tm_min, tp->tm_sec);
}

void insert(sqlite3 *db)
{
    char name[32] = {};
    float weight;
    float price;
    char sql[N] = {};
    char *errmsg;
    char buf[N] = {};

    printf("Input fruit name:");
    scanf("%s", name);
    getchar();

    printf("Input weight:");
    scanf("%f", &weight);

    printf("Input price:");
    scanf("%f", &price);

    gettime(buf);

    sprintf(sql, "insert into '%s' (name,weight,price,time)values('%s', '%.3f', '%.3f','%s')",TABLES, name, weight, price,buf);

    if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
    {
	printf("%s\n", errmsg);
    }
    else
    {
	printf("添加成功\n");
    }

}
int callback(void *arg, int f_num, char ** f_value, char ** f_name)
{

    if(flag == 1)
    {
	puts("");
	for(int j = 0; j < f_num; j++)
	{
	    printf("%-8s", f_name[j]);
	}

	flag = 0;
	puts("");
        puts("----------------------------------------");
    }

    for(int i = 0; i < f_num; i++ )
    {
	printf("%-8s", f_value[i]);
    }

    puts("");

    return 0;
}

void query(sqlite3 *db)
{
    char *errmsg;
    char sql[N] = {0};
    sprintf(sql, "select * from '%s'", TABLES);

    if(sqlite3_exec(db, sql, callback, NULL, &errmsg) != SQLITE_OK)
    {
	printf("%s\n", errmsg);
    }

}

void update_price(sqlite3 *db)
{
    float price;
    int id;
    char sql[N] = {};
    char *errmsg;

    printf("Input id:");
    scanf("%d", &id);

    printf("Input alter price:");
    scanf("%f", &price);

    sprintf(sql, "update '%s' set price='%f' where id=%d",TABLES , price,id);

    if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
    {
	printf("%s\n", errmsg);
    }
    else
    {
	printf("修改价格成功\n");
    }

}

void update_weight(sqlite3 *db)
{
    int id;
    char sql[N] = {};
    char *errmsg;
    float weight;
    char buf[N] = {};

    printf("Input id:");
    scanf("%d", &id);

    printf("Input weight:");
    scanf("%f", &weight);

    gettime(buf);

    sprintf(sql, "update '%s' set weight='%f',time='%s' where id=%d",TABLES, weight,buf,id);

    if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
    {
	printf("%s\n", errmsg);
    }
    else
    {
	printf("修改重量成功.\n");
    }

}
